﻿using System.Windows;
using CarAppClient.Extension;
using CarAppDto.Common;
using D.WPF.MVVM;
using D.WPF.MVVM.EventUtil;
using DelegateCommand = D.WPF.MVVM.EventUtil.DelegateCommand;

namespace CarAppClient.ViewModels
{
    public abstract class BaseModifyViewModel<T> : BaseMvvm where T : Window
    {
        /// <summary>
        /// 修改的页面
        /// </summary>
        protected Window? _page;

        private bool isAdd = true;
        /// <summary>
        /// 是否是新增操作，如果不是，部分表单功能不可用
        /// </summary>
        public bool IsAdd
        {
            get { return isAdd; }
            set { isAdd = value; RaisePropertyChanged(); }
        }

        private string? id;
        /// <summary>
        /// 主键
        /// </summary>
        public string? Id
        {
            get { return id; }
            set { id = value; RaisePropertyChanged(); }
        }

        public ViewStatus? _viewStatus;

        public BaseModifyViewModel(T? owner, string? id, ViewStatus? viewStatus)
        {
            Id = id;
            IsAdd = id == null;
            _page = owner;
            _viewStatus=viewStatus;
            LoadData();
            SetContolStatus();
        }

        protected virtual void LoadData()
        {

        }
      
        #region 确定事件    
        /// <summary>
        /// 提交事件
        /// </summary>
        private DelegateCommand? _submitCommand;
        /// <summary>
        /// 提交事件
        /// </summary>
        public DelegateCommand SubmitCommand => _submitCommand ?? (_submitCommand = new DelegateCommand(Submit));
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="obj"></param>
        protected virtual void Submit(object? obj)
        {
       
        }
        #endregion

        #region 取消事件

        /// <summary>
        /// 取消事件
        /// </summary>
        private DelegateCommand? _cancelCommand;
        /// <summary>
        /// 取消事件
        /// </summary>
        public DelegateCommand CancelCommand => _cancelCommand ?? (_cancelCommand = new DelegateCommand(Cancel));
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="obj"></param>
        protected virtual void Cancel(object? obj)
        {
            if (_page != null)
            {
                _page.Close();
            }
        }

        #endregion
        /// <summary>
        /// 窗体关闭
        /// </summary>
        public void FromClose()
        {
            _page.Close();
        }


        #region 查询
        /// <summary>
        /// 查询事件
        /// </summary>
        private DelegateCommandT<string>? _queryCommand;
        /// <summary>
        /// 查询事件
        /// </summary>
        public DelegateCommandT<string> QueryCommand => _queryCommand ?? (_queryCommand = new DelegateCommandT<string>(Query));
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="obj"></param>
        protected virtual void Query(string? type = null) { }
        #endregion

        #region 操作按钮显示隐藏权限

        private Visibility submitdBtnVisibility = Visibility.Visible;
        /// <summary>
        ///确定按钮状态
        /// </summary>
        public Visibility SubmitBtnVisibility
        {
            get { return submitdBtnVisibility; }
            set { submitdBtnVisibility = value; RaisePropertyChanged(); }
        }

        private Visibility cancelBtnVisibility = Visibility.Visible;
        /// <summary>
        /// 取消按钮状态
        /// </summary>
        public Visibility CancelBtnVisibility
        {
            get { return cancelBtnVisibility; }
            set { cancelBtnVisibility = value; RaisePropertyChanged(); }
        }


        #endregion

        #region 操作按钮状态


        private bool submitBtnEnabled = true;
        /// <summary>
        /// 确定按钮状态
        /// </summary>
        public bool SubmitBtnEnabled
        {
            get { return submitBtnEnabled; }
            set { submitBtnEnabled = value; RaisePropertyChanged(); }
        }


        private bool cancelBtnEnabled = true;
        /// <summary>
        /// 取消按钮状态
        /// </summary>
        public bool CancelBtnEnabled
        {
            get { return cancelBtnEnabled; }
            set { cancelBtnEnabled = value; RaisePropertyChanged(); }
        }

        private bool commonTextEnabled = true;
        /// <summary>
        /// 通用文本是否可编辑
        /// </summary>
        public bool CommonTextEnabled
        {
            get { return commonTextEnabled; }
            set { commonTextEnabled = value; RaisePropertyChanged(); }
        }
        #endregion
        protected virtual void SetContolStatus()
        {
            if (_viewStatus == ViewStatus.View)
            {
                CommonTextEnabled = false;
                SubmitBtnVisibility = Visibility.Hidden;
                CancelBtnVisibility = Visibility.Hidden;
            }
        }
        }
    }
